package com.lcp.utils;

import com.lcp.entity.Point2D;

/**
 * GeoMathUtil 空间数学工具类
 */
public class GeoMathUtil {

    private GeoMathUtil(){}
    /**
     * 功能描述: 获取两点之间线段中距离起点h长度某点<br>
     * @Param: [posS, posE, h]
     * @Return: com.lcp.entity.Point2D
     * @Author: Administrator
     * @Date: 2021/01/19 13:24
     */
    public static Point2D pointInLine(Point2D posS,Point2D posE,double h){
        double l = getDistance(posS,posE);
        double x = getOneAxisPos(posS.getX(),posE.getX(), h, l);
        double y = getOneAxisPos(posS.getY(),posE.getY(), h, l);
        return Point2D.toInstance(x,y);
    }

    public static double getDistance(Point2D start,Point2D end){
        double dx = start.getX() - end.getX();
        double dy = start.getY() - end.getY();
        return Math.sqrt(dx*dx+dy*dy);
    }

    private static double getOneAxisPos(double start,double  end,double  h,double  l){
            return start - h * (start - end) / l;
    }

}
